﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace xyw.admincp
{
    public partial class FriendesClassify : System.Web.UI.Page
    {
        XYW.BLL.News_Classify bllClass = new XYW.BLL.News_Classify();
        XYW.BLL.News bllNews = new XYW.BLL.News();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            this.Repeater1.DataSource = bllClass.GetList("type=1");
            this.Repeater1.DataBind();
            Generate();
        }

        private void Generate()
        {
            foreach (RepeaterItem ri in Repeater1.Items)
            {
                string classify = (ri.FindControl("HiddenField1") as HiddenField).Value;
                (ri.FindControl("Label1") as Label).Text = bllNews.GetRecordCount("classify='" + classify + "'").ToString() + "篇";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.txt_classifyName.Text == "")
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "nosr", "<script>alert('请输入分类名字！')</script>");
                return;
            }
            XYW.Model.News_Classify modelClass = new XYW.Model.News_Classify();
            int id = int.Parse(this.hd_Id.Value);
            if (id > 0)
            {
                modelClass = bllClass.GetModel(id);
            }
            modelClass.classifyName = this.txt_classifyName.Text.Trim();
            if (id > 0)
                bllClass.Update(modelClass);
            else
                bllClass.Add(modelClass);
            BindData();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                XYW.Model.News_Classify model = bllClass.GetModel(id);
                if (bllNews.GetRecordCount("classify='" + model.classifyName + "'") > 0)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "nor", "<script>alert('该类下存在新闻，不允许删除！')</script>");
                    return;
                }
                bllClass.Delete(id);
                BindData();
            }
        }
    }
}